﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;
using System.Data;
using TST.SharePointObjects.WebControls;
using Microsoft.SharePoint.Administration;
using System.Data.SqlClient;
using System.IO;
using Microsoft.SharePoint.Utilities;

namespace TST.SharePointObjects
{
    public class DatabaseSetupPage : SOPageBase
    {
        protected Label labelVersion;
        protected Label labelServer;
        protected Label labelDatabase;
        protected Label labelStatus;
        protected Label labelCreated;
        protected Label labelCreatedBy;
        protected Button buttonCreatePage;
        protected Button buttonCancel;
        protected Button buttonDelete;
        protected Label labelEndDate;
        protected Label labelLicenseType;
        private string _action;
        protected InputFormSection licensingSection;

        protected override bool RequireSiteAdministrator
        {
            get
            {
                return true;
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            try
            {
                licensingSection.Visible = false;
                InitUI();
            }
            catch (Exception ex)
            {
                labelStatus.Text = string.Format("General error while getting the information: {0}.", ex.Message);
                labelStatus.ForeColor = System.Drawing.Color.Red;
                labelStatus.Font.Bold = true;
            }
            buttonCreatePage.Enabled = !string.IsNullOrEmpty(_action);
            buttonCreatePage.Click += new EventHandler(buttonCreatePage_Click);
            buttonCancel.Click += new EventHandler(buttonCancel_Click);
            buttonDelete.Click += new EventHandler(buttonDelete_Click);
            base.OnLoad(e);
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            SPUtility.Redirect(SPContext.Current.Web.ServerRelativeUrl, SPRedirectFlags.UseSource, System.Web.HttpContext.Current);
        }

        private void InitUI()
        {
            _action = string.Empty;
            labelVersion.Text = "N/A";
            labelCreated.Text = "N/A";
            labelCreatedBy.Text = "N/A";
            labelLicenseType.Text = "N/A";
            labelEndDate.Text = "N/A";
            labelDatabase.Text = "N/A";
            labelServer.Text = "N/A";
            if (Database == null)
            {
                buttonCreatePage.Text = "Create";
                _action = "create";
                labelStatus.Text = "Database does not exist. Click the 'Create' button to create the database.";
                labelStatus.ForeColor = System.Drawing.Color.Red;
                labelStatus.Font.Bold = true;
            }
            else if (!Database.Exists)
            {
                buttonCreatePage.Text = "Configure";
                _action = "configure";
                labelStatus.Text = "Database has not been configured. Click the 'Configure' button to configure the database.";
                labelStatus.ForeColor = System.Drawing.Color.Orange;
                labelStatus.Font.Bold = true;
            }
            else if (Database.NeedsUpgrade) //(version.DatabaseVersion < Database.CURRENTDATABASEVERSION)
            {
                buttonCreatePage.Text = "Upgrade";
                _action = "upgrade";
                labelStatus.Text = "Database needs an upgrade. Click the 'Upgrade' button to upgrade the database to the latest version.";
                labelVersion.Text = Database.VersionInfo.DatabaseVersion.ToString();
                labelCreated.Text = Database.VersionInfo.Created.ToString(SPContext.Current.Web.Locale);
                labelCreatedBy.Text = Database.VersionInfo.CreatedBy;
                labelStatus.ForeColor = System.Drawing.Color.Orange;
                labelStatus.Font.Bold = true;
                labelLicenseType.Text = Database.VersionInfo.License.LicensingMode.ToString();
                labelEndDate.Text = Database.VersionInfo.License.EndDate.ToString(SPContext.Current.Web.Locale);
            }
            else
            {
                buttonCreatePage.Text = "OK";
                _action = "close";
                labelStatus.Text = "Database is up to date. No further action required.";
                labelVersion.Text = Database.VersionInfo.DatabaseVersion.ToString();
                labelCreated.Text = Database.VersionInfo.Created.ToString(SPContext.Current.Web.Locale);
                labelCreatedBy.Text = Database.VersionInfo.CreatedBy;
                labelStatus.ForeColor = System.Drawing.Color.Green;
                labelStatus.Font.Bold = true;
                labelLicenseType.Text = Database.VersionInfo.License.LicensingMode.ToString();
                labelEndDate.Text = Database.VersionInfo.License.EndDate.ToString(SPContext.Current.Web.Locale);
            }
            if (Database != null)
            {
                labelDatabase.Text = Database.Name;
                labelServer.Text = Database.Server.Name;
            }
        }

        private void buttonCreatePage_Click(object sender, EventArgs e)
        {
            switch (_action)
            {
                case "create":
                    Database = SharePointObjectsDatabase.Create();
                    Database.Update();
                    if (!Database.Exists)
                    {
                        Database.Provision();
                    }
                    break;
                case "configure":
                    if (!Database.Exists)
                    {
                        Database.Provision();
                    }
                    break;
                case "upgrade":
                    if (Database.NeedsUpgrade)
                    {
                        Database.Upgrade();
                    }
                    break;
                case "close":
                    SPUtility.Redirect(SPContext.Current.Web.ServerRelativeUrl, SPRedirectFlags.UseSource, System.Web.HttpContext.Current);
                    break;
            }
            SPUtility.Redirect(Page.Request.Url.ToString(), SPRedirectFlags.UseSource, System.Web.HttpContext.Current);
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            if (Database.Exists)
            {
                Database.Unprovision();
                Database.Delete();
            }
            SPUtility.Redirect(Page.Request.Url.ToString(), SPRedirectFlags.UseSource, System.Web.HttpContext.Current);
        }

    }
}
